package com.youbff.task.core;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.JobListener;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.batch.core.Job;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

import com.youbff.task.core.base.JobQuartz;

@Component
public class TaskDispatch {
    
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    /**
     * 添加工作任务
     * @author lixuefeng
     * @email youbff@163.com
     * @time 2018年1月26日 下午3:00:51
     * @param id
     * @param cron
     * @param execTask
     * @param jobListener
     * @throws SchedulerException
     */
    public void addJob(String id, String cron, Job execTask, JobListener jobListener) throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        String name = "task_" + id;
        String group = "group_" + id;
        TriggerKey triggerKey = TriggerKey.triggerKey(name, group);
        CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        // 不存在，创建一个
        JobDetail jobDetail = JobBuilder.newJob(JobQuartz.class).withIdentity(name, group).build();
        jobDetail.getJobDataMap().put("task", execTask);
        jobDetail.getJobDataMap().put("taskId", id);
        // 表达式调度构建器
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
        // 按新的表达式构建一个新的trigger
        trigger = TriggerBuilder.newTrigger().withIdentity(name, group).withSchedule(scheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.getListenerManager().addJobListener(jobListener);
    }
    
    /**
     * 移除工作任务
     * @author lixuefeng
     * @email youbff@163.com
     * @time 2018年2月1日 上午11:34:24
     * @param id
     * @throws SchedulerException
     */
    public void removeJob(String id) throws SchedulerException{
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        String name = "task_" + id;
        String group = "group_" + id;
        JobKey jobKey = JobKey.jobKey(name, group);
        scheduler.triggerJob(jobKey);
    }
    
    /**
     * 是否存在工作任务
     * @author lixuefeng
     * @email youbff@163.com
     * @time 2018年2月1日 下午3:03:26
     * @param id
     * @return ture:存在,false:不存在
     * @throws SchedulerException
     */
    public boolean isExistJob(String id) throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        String name = "task_" + id;
        String group = "group_" + id;
        TriggerKey triggerKey = TriggerKey.triggerKey(name, group);
        CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        if(trigger != null) {
            return true;
        }
        return false;
    }

}
